package gov.dhs.cis.soa.readers;

import gov.dhs.cis.soa.SOAData;
import gov.dhs.cis.soa.Utils;
import gov.dhs.cis.soa.data.Application;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class ApplicationReader extends ExcelReader {
	static final Logger loger = Logger.getLogger(ApplicationReader.class);
	
	private final String APP_COMP_FILE_NAME = "USCIS Architecture Components - Application Layer.xls";
	private String SHEET_NAME = "Unique Applications (used)";
	
	private String NAME_COL_NUM = "B";
	private String ST_COL_NUM = "C";
	private String IPT_COL_NUM = "D";
	private String APPNM_COL_NUM = "E";
	private String FUNC_COL_NUM = "H";
	private String TECH_COL_NUM = "M";
	private String DB_COL_NUM = "N";
	private String DISP_COL_NUM = "T";
	private String DISPREL_COL_NUM = "U";
	private String REL_COL_NUM = "AD";
	private String HOST_CNFG_COL_NUM = "AE";
	private String DA_COL_NUM = "AF";
	private String REL_TRANS_COL_NUM = "AG";
	
	private int MAX_ROWS = 230;
		
	public SOAData extractApplications(SOAData soaData) {
		loger.debug("Extracting Application layer Sheet...");
		HSSFWorkbook w = openFile(APP_COMP_FILE_NAME);
		HSSFSheet s1 = w.getSheet(SHEET_NAME);
		int rows = s1.getLastRowNum();
		loger.debug("S1 rows =" + rows);
		
		ArrayList<Application> appList = new ArrayList<Application>();
		soaData.appList = appList;
		
		for (int n = 2; n < MAX_ROWS; n++) {
			HSSFRow row = s1.getRow(n);
			if(row == null)
				continue;
			String name = getCellStringValue(w, row, NAME_COL_NUM);
			if(Utils.isEmpty(name)) {
				loger.warn("Application Name is Null/Empty at row : " + n + ", ignoring this row.");
				continue;
			}
			loger.debug("Reading Application [Row:" + n + "]: " + name);
			String rel2Transformation;
			try {
				rel2Transformation = getCellStringValue(w, row, REL_TRANS_COL_NUM);
			} catch (Exception e) {
				//loger.debug("Failed covert row <" + n + "> Ignoring, error msg:" + e.getLocalizedMessage());
				loger.warn("Failed covert cell [" + n + ", " + REL_TRANS_COL_NUM + "] Ignoring, error msg : " + e.getLocalizedMessage());
				continue;
			}
			//"In Transformation Solution".equalsIgnoreCase(rel2Transformation)
			if(Utils.isEmpty(rel2Transformation) || rel2Transformation.indexOf("Transforma") < 0) {
				loger.warn(name + " - is not In Transformation solution, so skipping it.");
				continue;
			}
			Application iObj = new Application();
			iObj.id = Utils.getNextId();
			iObj.name = name.trim();
			iObj.stereoType = getCellStringValue(w, row, ST_COL_NUM);
			if(Utils.isEmpty(iObj.stereoType))
				iObj.stereoType = Application.STEREO_TYPE;
			iObj.wipt = getCellStringValue(w, row, IPT_COL_NUM);
			iObj.appName = getCellStringValue(w, row, APPNM_COL_NUM);
			iObj.function = getCellStringValue(w, row, FUNC_COL_NUM);
			iObj.tech = getCellStringValue(w, row, TECH_COL_NUM);
			iObj.database = getCellStringValue(w, row, DB_COL_NUM);
			iObj.disposition = getCellStringValue(w, row, DISP_COL_NUM);
			iObj.dispRational = getCellStringValue(w, row, DISPREL_COL_NUM);
			iObj.relase = getCellStringValue(w, row, REL_COL_NUM);
			iObj.hostConfig = getCellStringValue(w, row, HOST_CNFG_COL_NUM);
			iObj.designAspects = getCellStringValue(w, row, DA_COL_NUM);
			iObj.relationship = rel2Transformation;
			iObj.createDescription();
			
			appList.add(iObj);
		}
		closeAll(w);
		return soaData;
	}
}
